package com.ruoyi.web.controller.system;

import java.util.Date;
import java.util.List;

import com.ruoyi.common.utils.uuid.UUID;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.system.domain.Xiaoxi;
import com.ruoyi.system.service.IXiaoxiService;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 站内邮箱Controller
 * 
 * @author zxl
 * @date 2023-04-03
 */
@Controller
@RequestMapping("/system/xiaoxi")
public class XiaoxiController extends BaseController
{
    private String prefix = "system/xiaoxi";

    @Autowired
    private IXiaoxiService xiaoxiService;
    @RequiresPermissions("system:xiaoxi:sendEmailview")
    @GetMapping("/sendEmail")
    public String sendEmail(String jieshouren,String jieshourenid,ModelMap mmap)
    {
        mmap.put("jieshouren", jieshouren);
        mmap.put("jieshourenid", jieshourenid);
        return prefix + "/sendEmail";
    }

    @RequiresPermissions("system:xiaoxi:sendBoxview")
    @GetMapping("/sendBox")
    public String sendBox()
    {
        return prefix + "/sendBox";
    }

    @RequiresPermissions("system:xiaoxi:inBoxview")
    @GetMapping("/inBox")
    public String inBox()
    {
        return prefix + "/inBox";
    }

    @RequiresPermissions("system:xiaoxi:saveBoxview")
    @GetMapping("/saveBox")
    public String saveBox()
    {
        return prefix + "/saveBox";
    }

    /**
     * 修改 站内邮箱-草稿箱-修改界面
     */
    @RequiresPermissions("system:xiaoxi:edit")
    @GetMapping("/edit/{xiaoxiid}")
    public String edit(@PathVariable("xiaoxiid") String xiaoxiid, ModelMap mmap)
    {
        Xiaoxi xiaoxi = xiaoxiService.selectXiaoxiByXiaoxiid(xiaoxiid);
        mmap.put("xiaoxi", xiaoxi);
        return prefix + "/edit";
    }

    @GetMapping("/JieshourenTree")
    public String JieShourenTree()
    {
        return prefix + "/JieshourenTree";
    }

    /**
     * 查询站内邮箱列表
     */
    @RequiresPermissions("system:xiaoxi:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(Xiaoxi xiaoxi)
    {
        startPage();
        List<Xiaoxi> list = xiaoxiService.selectXiaoxiList(xiaoxi);
        return getDataTable(list);
    }
    /**
     * 查询 站内邮箱-发件箱-列表
     */
    @RequiresPermissions("system:xiaoxi:sendBoxlist")
    @PostMapping("/sendBoxlist")
    @ResponseBody
    public TableDataInfo sendBoxlist(Xiaoxi xiaoxi)
    {
        startPage();
        xiaoxi.setFasongrenid(getUserId().toString());
        xiaoxi.setZhuangtai(1);
        List<Xiaoxi> list = xiaoxiService.selectXiaoxiList(xiaoxi);
        return getDataTable(list);
    }

    /**
     * 查询 站内邮箱-草稿箱-列表
     */
    @RequiresPermissions("system:xiaoxi:saveBoxlist")
    @PostMapping("/saveBoxlist")
    @ResponseBody
    public TableDataInfo saveBoxlist(Xiaoxi xiaoxi)
    {
        startPage();
        xiaoxi.setFasongrenid(getUserId().toString());
        xiaoxi.setZhuangtai(0);
        List<Xiaoxi> list = xiaoxiService.selectXiaoxiList(xiaoxi);
        return getDataTable(list);
    }
    /**
     * 查询 站内邮箱-收件箱-列表
     */
//    @RequiresPermissions("system:xiaoxi:inBoxlist")
    @PostMapping("/inBoxlist")
    @ResponseBody
    public TableDataInfo inBoxlist(Xiaoxi xiaoxi)
    {
        startPage();
        xiaoxi.setJieshourenid(getUserId().toString());
        List<Xiaoxi> list = xiaoxiService.selectInBoxList(xiaoxi);
        return getDataTable(list);
    }

    @PostMapping("/inBoxWeidulist")
    @ResponseBody
    public TableDataInfo inBoxWeidulist(Xiaoxi xiaoxi)
    {
        startPage();
        xiaoxi.setJieshourenid(getUserId().toString());
        List<Xiaoxi> list = xiaoxiService.selectInBoxWeiduList(xiaoxi);
        return getDataTable(list);
    }
    /**
     * 新增 站内邮箱-发送邮件-发送邮件/保存草稿
     */
    @RequiresPermissions("system:xiaoxi:add")
    @Log(title = "站内邮箱-发送邮件-发送邮件/保存草稿", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(Xiaoxi xiaoxi)
    {
        xiaoxi.setXiaoxiid(UUID.randomUUID().toString());
        xiaoxi.setShifouzhiding(0l);
        xiaoxi.setFasongren(getSysUser().getUserName());
        xiaoxi.setFasongrenid(getUserId().toString());
        xiaoxi.setFasongshijian(new Date());
        if(xiaoxi.getZhuangtai()==1){
            xiaoxiService.insertXiaoxiJieshouren(xiaoxi.getXiaoxiid(),xiaoxi.getJieshourenid());
        }
        return toAjax(xiaoxiService.insertXiaoxi(xiaoxi));
    }


    /**
     * 修改 站内邮箱-草稿箱-修改
     */
    @RequiresPermissions("system:xiaoxi:edit")
    @Log(title = "站内邮箱-草稿箱-修改", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(Xiaoxi xiaoxi)
    {
        if(xiaoxi.getZhuangtai()==1) {
            xiaoxi.setFasongshijian(new Date());
            xiaoxiService.insertXiaoxiJieshouren(xiaoxi.getXiaoxiid(),xiaoxi.getJieshourenid());
        }
        return toAjax(xiaoxiService.updateXiaoxi(xiaoxi));
    }

    /**
     * 删除 站内邮箱-发件箱-删除
     */
    @RequiresPermissions("system:xiaoxi:sendBoxremove")
    @Log(title = "站内邮箱-发件箱-删除", businessType = BusinessType.DELETE)
    @PostMapping( "/sendBoxremove")
    @ResponseBody
    public AjaxResult sendBoxremove(String ids)
    {
        return toAjax(xiaoxiService.deleteXiaoxiByXiaoxiids(ids));
    }

    /**
     * 删除 站内邮箱-收件箱-删除
     */
    @RequiresPermissions("system:xiaoxi:inBoxremove")
    @Log(title = "站内邮箱-收件箱-删除", businessType = BusinessType.DELETE)
    @PostMapping( "/inBoxremove")
    @ResponseBody
    public AjaxResult InBoxremove(String ids)
    {
        return toAjax(xiaoxiService.deleteXiaoxiJieshourenByXiaoxiids(ids,getUserId()));
    }

    /**
     * 删除 站内邮箱-草稿箱-删除
     */
    @RequiresPermissions("system:xiaoxi:saveBoxremove")
    @Log(title = "站内邮箱-草稿箱-删除", businessType = BusinessType.DELETE)
    @PostMapping( "/SaveBoxremove")
    @ResponseBody
    public AjaxResult SaveBoxremove(String ids)
    {
        return toAjax(xiaoxiService.deleteXiaoxiByids(ids));
    }



    /**
     * 修改 站内邮箱-收件箱-阅读
     */

    @GetMapping("/Yuedu")
    @ResponseBody
    public AjaxResult Yuedu(String xiaoxiid)
    {
        return toAjax(xiaoxiService.Yuedu(xiaoxiid,String.valueOf(getUserId())));
    }
}
